package com.infinit.easyreader.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nononsenseapps.filepicker.FilePickerFragment;
import com.nononsenseapps.filepicker.LogicHandler;

import java.io.File;

/**
 * 类： MyFilePickerFragment [V 1.0.0]
 * 包含类 : com.infinit.easyreader.fragment.MyFilePickerFragment
 * 作用：用于筛选出TXT文件
 * Created by rzy on 2016/2/1.
 */
public class MyFilePickerFragment extends FilePickerFragment {
    //定义后缀
    private static final String EXTENSION = ".txt";


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        if (view != null) {
            Button btnCancel = (Button) view.findViewById(com.nononsenseapps.filepicker.R.id.nnf_button_cancel);
            btnCancel.setText("取消");
            btnCancel.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
            Button btnOk = (Button) view.findViewById(com.nononsenseapps.filepicker.R.id.nnf_button_ok);
            btnOk.setText("确认");
            btnCancel.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
        }
        return view;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = super.onCreateViewHolder(parent, viewType);


        //设置ICON的大小
        View v = holder.itemView;
        //设置文字的大小
        TextView textView = (TextView) v.findViewById(android.R.id.text1);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
        //设置ICON的大小
        ImageView imageView = (ImageView) v.findViewById(com.nononsenseapps.filepicker.R.id.item_icon);
        ViewGroup.LayoutParams params = imageView.getLayoutParams();
        params.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 26, getResources().getDisplayMetrics());
        params.width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 32, getResources().getDisplayMetrics());
        imageView.setLayoutParams(params);
        imageView.setTranslationY(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources().getDisplayMetrics()));
        //设置container的属性
        LinearLayout container = (LinearLayout) v.findViewById(com.nononsenseapps.filepicker.R.id.nnf_item_container);
        int padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources().getDisplayMetrics());
        container.setPadding(padding, padding, padding, 0);
        ViewGroup.LayoutParams cParams = container.getLayoutParams();
        cParams.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 42, getResources().getDisplayMetrics());
        cParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        container.setLayoutParams(cParams);

        switch (viewType) {
            case LogicHandler.VIEWTYPE_HEADER:
                return new HeaderViewHolder(v);
            case LogicHandler.VIEWTYPE_CHECKABLE:
                return new CheckableViewHolder(v);
            case LogicHandler.VIEWTYPE_DIR:
            default:
                return new DirViewHolder(v);
        }
    }


    /**
     * 作用：获取文件的后缀名
     *
     * @param file 文件
     * @return 文件的后缀，无则返回null
     */
    private String getExtension(@NonNull File file) {
        String path = file.getPath();
        int index = path.lastIndexOf(".");
        if (index < 0) {
            return null;
        } else {
            return path.substring(index);
        }
    }

    @Override
    protected boolean isItemVisible(File file) {
        boolean ret = super.isItemVisible(file);
        if (ret && !isDir(file) && (mode == MODE_FILE || mode == MODE_FILE_AND_DIR)) {
            ret = EXTENSION.equalsIgnoreCase(getExtension(file));
        }
        return ret;
    }


    @Override
    public void onClickOk(View view) {
        if (mListener == null) {
            return;
        }

        // Some invalid cases first
        if ((allowMultiple || mode == MODE_FILE) && mCheckedItems.isEmpty()) {
            if (mToast == null) {
                mToast = Toast.makeText(getActivity(), "请至少选择一个文件！",
                        Toast.LENGTH_SHORT);
            }
            mToast.show();
            return;
        }

        if (allowMultiple) {
            mListener.onFilesPicked(toUri(mCheckedItems));
        } else if (mode == MODE_FILE) {
            mListener.onFilePicked(toUri(getFirstCheckedItem()));
        } else if (mode == MODE_DIR) {
            mListener.onFilePicked(toUri(mCurrentPath));
        } else {
            // single FILE OR DIR
            if (mCheckedItems.isEmpty()) {
                mListener.onFilePicked(toUri(mCurrentPath));
            } else {
                mListener.onFilePicked(toUri(getFirstCheckedItem()));
            }
        }
    }
}
